/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.homesoft.catalogman.gui;

import com.homesoft.catalogman.entity.HmObjectMeta;
import java.awt.Color;
import java.awt.FlowLayout;
import java.util.List;
import java.util.Map;
import org.jdesktop.swingx.JXPanel;

/**
 * EditorPane
 *
 * @author phucan
 */
public class EditorPane extends CustomPanel {

    public static final int MODE_SAVED = 0;
    public static final int MODE_ADD = 1;
    public static final int MODE_EDIT = 2;
    private EditorAction _action;

    /**
     * Creates new form EditorPane
     */
    public EditorPane() {
        initComponents();   
    
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlHeader = new org.jdesktop.swingx.JXPanel();
        lnkEdit = new org.jdesktop.swingx.JXHyperlink();
        lnkSave = new org.jdesktop.swingx.JXHyperlink();
        itemEditor1 = new com.homesoft.catalogman.gui.ItemEditor();

        setOpaque(false);
        setLayout(new java.awt.BorderLayout());

        pnlHeader.setBorder(null);
        pnlHeader.setMinimumSize(new java.awt.Dimension(0, 20));
        pnlHeader.setOpaque(false);
        pnlHeader.setPreferredSize(new java.awt.Dimension(287, 26));
        pnlHeader.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        lnkEdit.setForeground(new java.awt.Color(245, 245, 245));
        lnkEdit.setText("Edit");
        lnkEdit.setClickedColor(new java.awt.Color(245, 245, 245));
        lnkEdit.setFont(lnkEdit.getFont().deriveFont(lnkEdit.getFont().getStyle() | java.awt.Font.BOLD));
        lnkEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lnkEditActionPerformed(evt);
            }
        });
        pnlHeader.add(lnkEdit);

        lnkSave.setForeground(new java.awt.Color(245, 245, 245));
        lnkSave.setText("Save");
        lnkSave.setClickedColor(new java.awt.Color(245, 245, 245));
        lnkSave.setFont(lnkSave.getFont().deriveFont(lnkSave.getFont().getStyle() | java.awt.Font.BOLD));
        lnkSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lnkSaveActionPerformed(evt);
            }
        });
        pnlHeader.add(lnkSave);

        add(pnlHeader, java.awt.BorderLayout.NORTH);

        itemEditor1.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        itemEditor1.setOpaque(false);
        add(itemEditor1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void lnkEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lnkEditActionPerformed

        switchMode(MODE_EDIT);
        //TODO: write your code here
    }//GEN-LAST:event_lnkEditActionPerformed

    private void lnkSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lnkSaveActionPerformed

        if (_action == null) {
            switchMode(MODE_EDIT);
            return;
        }
        if (_action.saveData()) {
            switchMode(MODE_SAVED);
        }
        //TODO: write your code here
    }//GEN-LAST:event_lnkSaveActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.homesoft.catalogman.gui.ItemEditor itemEditor1;
    private org.jdesktop.swingx.JXHyperlink lnkEdit;
    private org.jdesktop.swingx.JXHyperlink lnkSave;
    private org.jdesktop.swingx.JXPanel pnlHeader;
    // End of variables declaration//GEN-END:variables

    /**
     * Set meta data
     *
     * @param objData
     */
    @Override
    public void setFormData(Object objData) {
        itemEditor1.setFieldsMeta((List<HmObjectMeta>) objData);
    }

    @Override
    public Object getFormData() {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        return null;
    }

    @Override
    public void onLoad() {
        itemEditor1.loadComponents();
    }

    /**
     * Gán giá trị cho một trường
     *
     * @param strField tên trường
     * @param objValue giá trị
     */
    public void setValue(String strField, Object objValue) {
        itemEditor1.setValue(strField, objValue);
    }

    /**
     * Gán giá trị cho một tập hợp trường
     *
     * @param data
     */
    public void setValue(Map<String, Object> data) {
        for (String k : data.keySet()) {
            itemEditor1.setValue(k, data.get(k));
        }
    }

    /**
     * Get field value
     *
     * @param strField field name
     * @return Object
     */
    public Object getValue(String strField) {
        return itemEditor1.getValue(strField);
    }

    /**
     * Get field value
     *
     * @param <T> Data type to get
     * @param strField object value
     * @return strong type value
     */
    public <T extends Object> T getStrongTypeValue(String strField) {
        return itemEditor1.getStrongTypeValue(strField);
    }

    public void setAction(EditorAction action) {
        this._action = action;
    }

    public interface EditorAction {

        /**
         * Save data
         *
         * @return Return TRUE if process is successful
         */
        public abstract boolean saveData();
    }

    public class EditorActionImpl implements EditorAction {

        @Override
        public boolean saveData() {
            return true;
        }
    }

    /**
     * Swith editor mode
     *
     * @param iEditMode editor mode
     */
    public void switchMode(int iEditMode) {
        switch (iEditMode) {
            case MODE_EDIT:
            case MODE_ADD:
                lnkEdit.setText("Cancel");
                lnkSave.setVisible(true);
                itemEditor1.setEditable(true);
                break;

            case MODE_SAVED:
                lnkEdit.setText("Edit");
                lnkSave.setVisible(false);
                itemEditor1.setEditable(false);
                break;
        }

    }
}
